package com.jftone.bh.app.service;

import java.util.List;

import com.jftone.bh.app.config.SysConst;
import com.jftone.bh.app.model.SysMenu;
import com.jftone.bh.app.model.SysMenuFunc;
import com.jftone.bh.app.model.SysRight;
import org.apache.log4j.Logger;
import org.jftone.annotation.Autowired;
import org.jftone.annotation.Service;
import org.jftone.annotation.Transactional;
import org.jftone.dao.Dao;
import org.jftone.exception.ComponentException;
import org.jftone.exception.DaoException;
import org.jftone.util.DataMap;
import org.jftone.util.IData;


@Service
public class SysMenuService {
	
	private Logger logger = Logger.getLogger(SysMenuService.class);
	
	@Autowired
	private Dao dao;
	
	public List<SysMenu> querySysMenu(IData<String, Object> inData) throws ComponentException{
		List<SysMenu> menuList = null;
		try{
			menuList = dao.query(SysMenu.class, inData);
		}catch(DaoException e){
			logger.error("查询菜单根节点数据错误"+e.getMessage(), e);
			throw new ComponentException("查询菜单根节点数据错误"+e.getMessage(), e);
		}
		return menuList;
	}
	
	public SysMenu getSysMenu(IData<String, Object> inData) throws ComponentException{
		SysMenu sysMenu = null;
		try{
			sysMenu = dao.get(SysMenu.class, inData);
		}catch(DaoException e){
			logger.error("查询菜单根节点数据错误"+e.getMessage(), e);
			throw new ComponentException("查询菜单根节点数据错误"+e.getMessage(), e);
		}
		return sysMenu;
	}
	
	public IData<String, Object> getMenuByParentCode(String parentCode) throws ComponentException{
		IData<String, Object> retData = null;
		try{
			IData<String, Object> inData = new DataMap<String, Object>();
			inData.put("MENU_CODE", parentCode);
			retData = dao.get("queryMenuByCode", inData);
		}catch(DaoException e){
			logger.error("查询菜单根节点数据错误"+e.getMessage(), e);
			throw new ComponentException("查询菜单根节点数据错误"+e.getMessage(), e);
		}
		return retData;
	}
	
	public List<SysMenuFunc> querySysMenuFunc(IData<String, Object> inData) throws ComponentException{
		List<SysMenuFunc> menuFuncList = null;
		try{
			menuFuncList = dao.query(SysMenuFunc.class, inData);
		}catch(DaoException e){
			logger.error("查询菜单功能数据错误" + e.getMessage(), e);
			throw new ComponentException("查询菜单功能数据错误" + e.getMessage(), e);
		}
		return menuFuncList;
	}
	
	@Transactional
	public int deleteMenu(int menuId) throws ComponentException{
		int resultCode = 0;
		try{
			IData<String, Object> inData = new DataMap<String, Object>();
			inData.put("id", menuId);
			SysMenu menu = dao.get(SysMenu.class, inData);
			if(menu == null){
				return 1;
			}
			inData.clear();
			inData.put("parentCode", menu.getMenuCode());
			if(dao.count(SysMenu.class, inData)>0){
				return 2;
			}
			inData.clear();
			inData.put("menuCode", menu.getMenuCode());
			if(dao.count(SysRight.class, inData)>0){
				return 3;
			}
			dao.delete(SysMenuFunc.class, inData);
			dao.delete(menu);
		}catch(DaoException e){
			logger.error("删除菜单数据错误："+e.getMessage(), e);
			throw new ComponentException("删除菜单数据错误："+e.getMessage(), e);
		}
		return resultCode;
	}
	
	/**
	 * 保存菜单数据
	 * @param menu
	 * @param smfList
	 * @return
	 * @throws ComponentException
	 */
	@Transactional
	public int saveMenu(int menuNode, SysMenu menu, List<SysMenuFunc> smfList) throws ComponentException{
		int resultCode = 0;
		
		try{
			IData<String, Object> inData = new DataMap<String, Object>();
			inData.put("menuCode", menu.getMenuCode());
			//如果id为空，表示新增，需要校验是否有重复情况
			if(null == menu.getId()){
				if(dao.count(SysMenu.class, inData)>0){
					return 1;
				}
				dao.save(menu);
			}else{
				dao.update(menu);
				//如果是针对非节点菜单新增，需要清理之前新增的菜单功能数据
				if(menuNode != SysConst.MENU_NODE){
					dao.delete(SysMenuFunc.class, inData);
				}
			}
			if(null != smfList && smfList.size()>0){
				dao.insertBatch(smfList);
			}
		}catch(DaoException e){
			logger.error("更新菜单数据错误："+e.getMessage(), e);
			throw new ComponentException("更新菜单数据错误："+e.getMessage(), e);
		}
		return resultCode;
	}
	
}
